﻿using DotnetWinFormApp.DB;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UtilZ.Dotnet.DBIBase;
using UtilZ.Dotnet.Ex.Config;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.ZLog;

namespace ConAppCore
{
    class Program
    {
        static void Main(string[] args)
        {
            InitLog();

            AddDBConfig();
            //Console.WriteLine("Dm");
            SQLServerTest.Test(3);
            //DmTest.Test(22);
            //SQLiteTest.Test(33);
            //PostgreSQLTest.Test(44);
            //MysqlTest.Test(55);
            //OracleTest.Test(66);//oracl安装后没能跑起来
            //Console.WriteLine("========================================================");

            Console.WriteLine("q exit");
            Console.ReadKey();
        }

        private static void AddDBConfig()
        {
            var dbConfig = new DatabaseConfig()
            {
                Account = "sa",
                CommandTimeout = -1,
                ConnectParaType = 0,
                ConnectionString = null,
                ConnectionTimeout = -1,
                DBFactory = @"UtilZ.Dotnet.DBSqlServer.SQLServerDBFactory,DBPlugins\SqlServer\*\UtilZ.Dotnet.DBSqlServer",
                DBID = 3,
                DatabaseName = "POSTURE",
                Extend = null,
                GetConTimeout = 10000,
                Host = "192.168.10.11",
                Name = "TSDB",
                Password="qweQWE123",
                Parser = null,
                Port = 1433,
                ReadConCount = -1,
                SqlMaxLength = -1,
                WriteConCount = -1,
            };
            DatabaseConfigManager.AddConfigItem(dbConfig);
        }


        private static void InitLog()
        {
            ZLoger.OutputOption = ZLogerOutputOptions.Loger;
            var redirectAppenderToUI = (RedirectAppender)Loger.GetAppenderByName(null, "RedirectToUI");
            if (redirectAppenderToUI != null)
            {
                redirectAppenderToUI.RedirectOuput += RedirectLogOutput;
            }
        }

        private static void RedirectLogOutput(object sender, RedirectOuputArgs e)
        {
            ConsoleColor fontColor;
            string str;
            try
            {
                var item = e.Item;
                str = string.Format("{0} {1}", e.Item.Time.ToString(LogConstant.DateTimeFormat), e.Item.Content);

                switch (item.Level)
                {
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Trace:
                        fontColor = ConsoleColor.Gray;
                        break;
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Debug:
                        fontColor = ConsoleColor.Gray;
                        break;
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Info:
                        fontColor = ConsoleColor.White;
                        break;
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Warn:
                        fontColor = ConsoleColor.Yellow;
                        break;
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Error:
                        fontColor = ConsoleColor.Red;
                        break;
                    case UtilZ.Dotnet.Ex.Log.LogLevel.Fatal:
                        fontColor = ConsoleColor.Red;
                        break;
                    default:
                        return;
                }
            }
            catch (Exception ex)
            {
                fontColor = ConsoleColor.Red;
                str = ex.Message;
            }

            var oldForegroundColor = Console.ForegroundColor;
            Console.ForegroundColor = fontColor;
            Console.WriteLine(str);
            Console.ForegroundColor = oldForegroundColor;
        }
    }
}
